<?php
require('lib/verify.php');
$_category = "wardkp";
$_title = "Home";
require('lib/head.php');

$forum_id = $pun_user["id"];
$system_id = $system["system_id"];
$sql = "select * from wm_characters where character_forum_id = '$forum_id' and character_active='1'";
$result = $db->query($sql);

$character = $db->fetch_assoc($result);

$is_new = 1;
if($db->num_rows($result) > 0) {
  $is_new = 0;
 }

if($is_new) {
  boxTop("Welcome to wardkp");
  echo 'Welcome to wardkp!</br>As this appears to be your first time using wardkp, please <b><a href="register-character.php">Register a Character</a></b> in a wardkp system.';
  boxBot();
 }
 else {
   if(!$character["character_approved"]) {
     boxTop("Character Approval Pending");
     echo 'An administrator for '.$system["system_name"].' has yet to approve your character.  Once this is done, you\'ll be able to access '.$system["system_name"].' wardkp.';
     echo "<br>";
     boxBot();
   }
   else {
     echo '<div id="leftpane" style="width: 100%;">';
     boxTop($system["system_name"] . " wardkp: " . "Current DKP Standings");
     $sort_phrase = $_GET["sort"];
     $flip = $_GET["flip"];
     if(!isset($sort_phrase)) {
       $sort_phrase = "character_class";
     }
     if(!isset($flip)) {
       $flip = 0;
     }

     ?>
       <table>
	  <tr>
	  <th width=35><a href="?sort=character_class<?php if($sort_phrase=="character_class") { echo "&flip=". (1-$flip);}?>">Class</a></th>
	  <th><a href="?sort=character_name<?php if($sort_phrase=="character_name") { echo "&flip=". (1-$flip);}?>">Character</a></th>

	  <th><a href="?sort=character_dkp_earned<?php if($sort_phrase=="character_dkp_earned") { echo "&flip=". (1-$flip);}?>">Earned</a></th>
	  <th><a href="?sort=character_dkp_spent<?php if($sort_phrase=="character_dkp_spent") { echo "&flip=". (1-$flip);}?>">Spent</a></th>
	  <th><a href="?sort=character_dkp<?php if($sort_phrase=="character_dkp") { echo "&flip=". (1-$flip);}?>">DKP</a></th>
	  </tr>

     <?php
     if($sort_phrase=="character_dkp" ||
        $sort_phrase=="character_dkp_earned" ||
        $sort_phrase=="character_dkp_spent") {
        $flip = 1 - $flip;
     }

     if($flip==1) {
       $flip_phrase = "desc";
     }
     else {
       $flip_phrase = "asc";
     }

     $sort_phrase = $db->escape($sort_phrase);

     $sql = "select * from wm_characters where character_approved = 1 and character_system_id = $system_id order by $sort_phrase $flip_phrase, character_class asc, character_dkp desc, character_name asc";
     $result = $db->query($sql);
     while($chara = $db->fetch_assoc($result)) {
       if($chara["character_dkp_satout"]==0 and $chara["character_dkp_earned"] == 0) {
	 continue;
       }
       ?>
	 <tr>
	    <td align="right"><?cclass($chara["character_class"])?></td>
	    <td><a href="character.php?id=<?=$chara["character_id"]?>"><?=$chara["character_name"]?></a></td>
	    <td align="right"><?=number_format($chara["character_dkp_earned"], 2)?></td>
	    <td align="right"><?=number_format($chara["character_dkp_spent"], 2)?></td>
	    <td align="right"><?=number_format($chara["character_dkp"], 2)?></td>
	    </tr>
	    <?php
	    }
     ?>
       </table>
	   <?php
	   boxBot();
     ?>
	   </div>


	       <br style="clear: both;" />



	       <?php
	       }
 }
?>
<?php
require('lib/foot.php');
?>

